// pages/order/index.js
import {checkoutOrder,dingdanSubmit} from '../../apis/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartIds:'',
    mode: '',
    order: {},
    address: {},
    personal: {},
    setting: {},
    addrDetail:'',
    goodsList: [],
    remark: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    let mode = options.mode
    let cartIds = decodeURIComponent(options.cartIds)
    console.log(mode, cartIds)
    this.setData({
      mode: mode,
      cartIds:cartIds
    })
    this.initData(mode, cartIds)
  },
  async initData(mode, cartIds){
      const res = await checkoutOrder({
        data: {
          mode: mode,
          delivery: 0,
          couponId: 0,
          isUsePoints: 0,
          cartIds: cartIds
        }
      })
      console.log('checkOrder---', res)
      let address =res.data.order.address
      let region = address.region
      let addrDetail = region.province+"/"+region.city+"/"+region.region+'/'+address.detail
      console.log('addr---', addrDetail)
      if (res.status === 200) {
         this.setData({
           order: res.data.order,
           goodsList: res.data.order.goodsList,
           personal: res.data.personal,
           setting: res.setting,
           addrDetail: addrDetail
         })
      }
  },
  async submit(){
    /*
      cartIds: "10010,10011,10013,10014,10015,10016"
      couponId: 0
      delivery: 10
      isUsePoints: 0
      mode: "cart"
      payType: 10
      remark: ""
    */ 
   const data = {
    cartIds: this.data.cartIds,
    couponId: 0,
    delivery: 10,
    isUsePoints: 0,
    mode: "cart",
    payType: 10,
    remark: this.data.remark
   }
   const res = await dingdanSubmit({data:data})
   console.log('res---', res)
   if(res.status === 200){
     wx.navigateTo({
       url: '/pages/dingdan/index',
     })
   }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})